package com.cloud.pole.merchant.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.constant.UserConstant;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.merchant.biz.ShopMerchantUserBiz;
import com.cloud.pole.merchant.model.entity.ShopMerchantUser;
import com.cloud.pole.merchant.service.ShopMerchantUserService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年8月5日
*cloud-shop-merchant
*author:frank-chen
*/
@Service
@Slf4j
public class ShopMerchantUserServiceImpl implements ShopMerchantUserService{

	@Autowired
	private ShopMerchantUserBiz userBiz;
	
    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);

	@Override
	public ShopMerchantUser getUserByMobile(String mobileArea, String mobile) throws Exception{
		// TODO Auto-generated method stub
		return userBiz.selectByMobile(mobileArea, mobile);
	}

	@Override
	public void addUser(ShopMerchantUser user) throws Exception {
		// TODO Auto-generated method stub
		userBiz.insertUseGeneratedKeys(user);
	}
	
    @Override
    public ShopMerchantUser getMyInfo(String token) throws Exception {
        String username = JwtTokenUtil.getInfoFromToken(token).getUniqueName();
        if (username == null) {
    		throw new BaseException(CommonConstants.TOKEN_ERROR_CODE, CommonConstants.TOKEN_ERROR_MSSSAGE);
        }
        ShopMerchantUser user = userBiz.selectUserByUsername(username);
        if(user == null) {
    		throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "用户不存在");
        }
        return user;
    }

	@Override
	public void modifyMyInfo(String token, ShopMerchantUser user) throws Exception {
		// TODO Auto-generated method stub
        String username = JwtTokenUtil.getInfoFromToken(token).getUniqueName();
        if (username == null) {
    		throw new BaseException(CommonConstants.TOKEN_ERROR_CODE, CommonConstants.TOKEN_ERROR_MSSSAGE);
        }
        ShopMerchantUser searchUser = userBiz.selectUserByUsername(username);
        if(searchUser == null) {
    		throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "用户不存在");
        }
        if(!StringUtils.equals(username, user.getUserName())) {
    		throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限修改");
        }
//        if(!StringUtils.isEmpty(user.getName())) {
//        	searchUser.setName(user.getName());
//        }
//        if(!StringUtils.isEmpty(user.getSex())) {
//        	searchUser.setSex(user.getSex());
//        }
        userBiz.updateSelectiveById(user);
	}

	@Override
	public void modifyMyPassWord(String token, String oldPwd, String newPwd) throws Exception {
		// TODO Auto-generated method stub
		String username = JwtTokenUtil.getInfoFromToken(token).getUniqueName();
		if (username == null) {
			throw new BaseException(CommonConstants.TOKEN_ERROR_CODE, CommonConstants.TOKEN_ERROR_MSSSAGE);
		}
		ShopMerchantUser searchUser = userBiz.selectUserByUsername(username);
		if (searchUser == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "用户不存在");
		}	
        String encoderNewPwd = new BCryptPasswordEncoder(UserConstant.PW_ENCORDER_SALT).encode(newPwd);
        if (!encoder.matches(oldPwd, searchUser.getPassword())) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "当前密码错误");
        }
        searchUser.setPassword(encoderNewPwd);
        userBiz.updateSelectiveById(searchUser);
	}
}
